package com.xmtlxx.web.controller.wms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.web.controller.wms.service.IPickService;
import com.xmtlxx.web.controller.wms.service.SmartCabinetService;
import com.xmtlxx.wms.domain.Pick;
import com.xmtlxx.wms.domain.bo.OutboundHandoverBo;
import com.xmtlxx.wms.domain.bo.PickBo;
import com.xmtlxx.wms.domain.bo.PickDtlBo;
import com.xmtlxx.wms.domain.bo.VerifyOutProBo;
import com.xmtlxx.wms.domain.vo.PickDtlVo;
import com.xmtlxx.wms.domain.vo.PickVo;
import com.xmtlxx.wms.service.IPickDtlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 挑拣任务Controller
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Validated
@Api(value = "挑拣任务控制器", tags = {"挑拣任务管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/pick")
public class PickController extends BaseController {

    private final IPickService iPickService;

    private final IPickDtlService iPickDtlService;

    private final SmartCabinetService smartCabinetService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<PickVo> initBean(PickBo bo) {

        PickVo vo = iPickService.initBeanPickVo(bo.getWid());
        //可以对数据进行初始化
        return R.ok(vo);
    }

    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBeanByDeal")
    R<PickVo> initBeanByDeal(PickBo bo) {

        PickVo vo = iPickService.initBeanDealPickVo(bo.getDealId());
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询挑拣任务列表
     */
    @ApiOperation("查询挑拣任务列表")
//    @SaCheckPermission("wms:pick:list")
    @GetMapping("/list")
    public TableDataInfo<PickVo> list(@Validated(QueryGroup.class) PickBo bo, PageQuery pageQuery) {

        return iPickService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出挑拣任务列表
     */
    @ApiOperation("导出挑拣任务列表")
    @SaCheckPermission("wms:pick:export")
    @Log(title = "挑拣任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated PickBo bo, HttpServletResponse response) {

        List<PickVo> list = iPickService.queryList(bo);
        ExcelUtil.exportExcel(list, "挑拣任务", PickVo.class, response);
    }

    /**
     * 获取挑拣任务详细信息
     */
    @ApiOperation("获取挑拣任务详细信息")
//    @SaCheckPermission("wms:pick:query")
    @GetMapping("/{id}")
    public R<PickVo> getInfo(@ApiParam("主键")
                             @NotNull(message = "主键不能为空")
                             @PathVariable("id") String id) {

        PickVo vo = iPickService.queryById(id);
        PickDtlBo bo = new PickDtlBo();
        bo.setPickId(id);
        bo.setDelFlag("0");
//        if(vo.getOperator() == null){
//            vo.setOperator(LoginHelper.getUserId());
//        }
        vo.setItems(iPickDtlService.queryList(bo));
        return R.ok(vo);
    }

    /**
     * 获取挑拣任务详细信息
     */
    @ApiOperation("获取挑拣任务详细信息")
//    @SaCheckPermission("wms:pick:query")
    @GetMapping("getTaskInfo/{id}")
    public R<List<PickVo>> getTaskInfo(@ApiParam("主键")
                                       @NotNull(message = "主键不能为空")
                                       @PathVariable("id") String id) {

        return R.ok(iPickService.queryByTaskId(id));
    }

    /**
     * 新增挑拣任务
     */
    @ApiOperation("新增挑拣任务")
    @SaCheckPermission("wms:pick:add")
    @Log(title = "挑拣任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Transactional(rollbackFor = Exception.class)
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PickVo vo) {

        PickBo bo = new PickBo();
        BeanUtil.copyProperties(vo, bo);
        Pick item = iPickService.insertByBo(bo);
        if (item != null) {
            iPickService.pushPickDatas(item.getId());
        }
        return toAjax(item != null ? 1 : 0);
    }

    /**
     * 修改挑拣任务
     */
    @ApiOperation("修改挑拣任务")
    @SaCheckPermission("wms:pick:edit")
    @Log(title = "挑拣任务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @Transactional(rollbackFor = Exception.class)
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PickVo vo) {

        PickBo bo = new PickBo();
        BeanUtil.copyProperties(vo, bo);
        Pick item = iPickService.updateTaksByBo(bo);
        if (item != null) {
            iPickService.pushPickDatas(item.getId());
        }
        return toAjax(item != null ? 1 : 0);
    }

    /**
     * 删除挑拣任务
     */
    @ApiOperation("删除挑拣任务")
    @SaCheckPermission("wms:pick:remove")
    @Log(title = "挑拣任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {

        return toAjax(iPickService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 挑拣任务-拆箱
     */
    @ApiOperation("挑拣任务-拆箱")
    @PostMapping("/unpacking/{boxId}/{id}")
    @Transactional(rollbackFor = Exception.class)
    public R<List<PickDtlVo>> unpacking(@ApiParam("主键串")
                                        @NotEmpty(message = "主键不能为空")
                                        @PathVariable String boxId, @PathVariable String id) {

        return R.ok(iPickService.deleteWithBoxByIds(boxId, id));
    }


    @ApiOperation("获取验证出库物品")
    @GetMapping("verifyOutPro/page")
    public TableDataInfo<VerifyOutProBo> getVerifyOutPro(PageQuery pageQuery, VerifyOutProBo bo) {

        return smartCabinetService.getVerifyOutPro(pageQuery, bo);
    }

    @ApiOperation("获取验证出库物品")
    @GetMapping("outboundHandover/page")
    @SaCheckPermission("app:wms:handover")
    public TableDataInfo<OutboundHandoverBo> getOutboundHandoverList(PageQuery pageQuery, OutboundHandoverBo bo) {

        return iPickService.getOutboundHandoverList(pageQuery, bo);
    }

}
